package com.gfeeling.controller;

import javax.annotation.Resource;

import org.apache.commons.configuration.ConfigurationException;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gfeeling.model.Log;
import com.gfeeling.model.PageBean;
import com.gfeeling.service.LogService;
import com.gfeeling.utils.DateUtils;
import com.gfeeling.utils.FormatJsonUtil;
import com.gfeeling.utils.JsonUtil;
import com.gfeeling.utils.PropertyApache;
import com.gfeeling.utils.QueryHelper;

/**
 * 
 * @author lsl
 * @date 2016年6月30日
 * @comment
 * @version 1.0
 */
@Controller
@RequestMapping("/log")
public class LogController extends BaseController {
	@Resource
	private LogService logService;

	@RequestMapping(value = "/index")
	public String index() {
		return "log/log-index";
	}

	@RequestMapping(value = "/list")
	public @ResponseBody PageBean<Log> list(@RequestParam(required = true,defaultValue="1") Integer page,
			@RequestParam(required = false) Integer pageSize,
			@RequestParam(required = false) String minDate,
			@RequestParam(required = false) String maxDate) throws NumberFormatException, ConfigurationException {
		QueryHelper queryHelper = new QueryHelper(Log.class, "l");
		if(StringUtils.hasText(minDate)){
			//时间格式转换
			queryHelper.addCondition("l.createTime>=?",DateUtils.parseDate(minDate));
		}
		if(StringUtils.hasText(maxDate)){
			//时间格式转换
			queryHelper.addCondition("l.createTime<=?",DateUtils.parseDate(maxDate));
		}
		//按创建时间降序排列
		queryHelper.addOrderProperty("l.createTime", false);
		PageBean<Log> pageBean;
		if(pageSize!=null){
			pageBean = this.logService.getPageBean(page, pageSize, queryHelper);
		}else{
			//如果没有传pageSize,默认读取配置文件里的参数
			pageBean = this.logService.getPageBean(page, Integer.parseInt(PropertyApache.getInstance().getProperty("pageSize")), queryHelper);
		}
		FormatJsonUtil.printJson(JsonUtil.getInstance().obj2json(pageBean));
		return pageBean;
	}
}
